package com.moon;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

import cn.yueliangbaba.R;

import com.sys.DemoApp;
import com.util.ImageLoaderHelper;
import com.widget.TouchImageView;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class ImageShowActivity extends BaseActivity {

	public ViewPager viewPager; // android-support-v4中的滑动组件
	private List<ImageView> imageViews; // 滑动的图片集合

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_show);

		findViewById(R.id.save).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				TouchImageView imageview = (TouchImageView)imageViews.get(viewPager.getCurrentItem());
				
				BitmapDrawable drawable = (BitmapDrawable) imageview.getDrawable();
				Bitmap bitmap = drawable.getBitmap();
				
				MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", "description");
				showToast("保存成功");
			}
		});
		
		init();
	}

	private void init() {
		imageViews = new ArrayList<ImageView>();
		viewPager = (ViewPager) findViewById(R.id.vp);
		viewPager.setOnPageChangeListener(new MyPageChangeListener());
		imageViews.clear();

		String img = getIntent().getStringExtra("imgs");
		if(img.length() > 0 && img.charAt(0) == ',') img = img.substring(1);
		String imgList[] = img.split(",");
		
		for (int i = 0; i < imgList.length; i++) 
		{
			TouchImageView imageView = new TouchImageView(this);
			imageView.setPadding(3, 0, 0, 0);
			imageView.setMaxZoom(4f);
			
			if(i < ((DemoApp)getApplicationContext()).mBitmapArray.size())
			{
				imageView.setImageBitmap(((DemoApp)getApplicationContext()).mBitmapArray.get(i));
			}
			
			if(imgList[i].contains("http://")) ImageLoaderHelper.displayImage(0, imageView,imgList[i]);
			else ImageLoaderHelper.displayImage(0, imageView,"http://120.27.129.233/ylbb/api/" + imgList[i]);
			imageViews.add(imageView);
		}

		viewPager.setAdapter(new MyAdapter());// 设置填充ViewPager页面的适配器
		viewPager.setCurrentItem(getIntent().getExtras().getInt("index"));
	}

	Timer mScaleTime = null;
	boolean mScale = false;

	// ==================shantu ===============

	/**
	 * 当ViewPager中页面的状态发生改变时调用
	 * 
	 * @author Administrator
	 * 
	 */
	private class MyPageChangeListener implements OnPageChangeListener {
		/**
		 * This method will be invoked when a new page becomes selected.
		 * position: Position index of the new selected page.
		 */
		public void onPageSelected(int position) {
		}

		public void onPageScrollStateChanged(int arg0) {

		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}
	}

	/**
	 * 填充ViewPager页面的适配器
	 * 
	 * @author Administrator
	 * 
	 */
	private class MyAdapter extends PagerAdapter {

		public MyAdapter() {
		}

		@Override
		public int getCount() {
			// return imageResId.length;
			return imageViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(imageViews.get(arg1));
			return imageViews.get(arg1);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView((View) arg2);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {

		}

		@Override
		public void finishUpdate(View arg0) {

		}
	}
}
